package com.tuanzi.loan.web.bean.receipt.user;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.entity.receipt.user.ReceiptSystemUser;
import com.tuanzi.loan.business.service.receipt.user.ReceiptSystemUserService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.receipt.user.ReceiptSystemUserSearchRequest;
import com.tuanzi.loan.vo.response.receipt.user.ReceiptSystemUserSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.receipt.user.ReceiptSystemUserApproveLazyDataModel;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author Ray 2017/09/21
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class ReceiptSystemUserApproveBean extends JSFListingBean<ReceiptSystemUserSearchRequest, ReceiptSystemUserSearchResponse> {

	private static final long serialVersionUID = 30992312030673477L;

	@Autowired
	private ReceiptSystemUserService receiptSystemUserService;

	@PostConstruct
	private void init() {
		initLazyDataModel();
	}

	public void onReset() {
		initLazyDataModel();
	}

	public void onDisable(String id) {
		ReceiptSystemUser receiptSystemUser = receiptSystemUserService.findOne(id);
		receiptSystemUserService.disable(receiptSystemUser);
		addSuccessfullyMessage();
	}

	public void onEnable(String id) {
		ReceiptSystemUser receiptSystemUser = receiptSystemUserService.findOne(id);
		receiptSystemUserService.enable(receiptSystemUser);
		addSuccessfullyMessage();
	}

	private List<ReceiptSystemUser> getSelectedUsers() {
		return receiptSystemUserService.findAll(selectedRecords.parallelStream().map(ReceiptSystemUserSearchResponse::getId).map(x -> x.toString()).collect(Collectors.toList()));
	}

	/*
	 * public String onAdd() { return
	 * redirect("/views/receipt/user/approve/info.xhtml"); }
	 */

	public String onEdit(String id) {
		return redirect("/views/receipt/user/approve/info.xhtml?id=" + id);
	}

	public void onUnlock(String id) {
		receiptSystemUserService.unlock(id);
		addSuccessfullyMessage();
	}

	private void initLazyDataModel() {
		$ = new ReceiptSystemUserSearchRequest();
		lazyDataModel = new ReceiptSystemUserApproveLazyDataModel($);
	}

	@Override
	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList(DictionaryCode.RECEIPT_SYSTEM_USER_STATUS);
	}
}
